/**
 * Created by bitfeng on 16/4/26.
 */
var webPage = require('webpage');
var page = webPage.create();

var url = "http://www.bjjs.gov.cn/tabid/763/default.aspx";
console.log(url);

// 打印页面标题
function print_title(page){
    var title = page.evaluate(function(){
        return document.title;
    });
    console.log(title);
}

// 获取当前页面
function get_current_page(page){
    var page_now = page.evaluate(function(){
        var val = document.getElementsByClassName('fk fk_cur')[0];
        return val.parentNode.getElementsByClassName('pc')[0].textContent;
    });
    return page_now.textContent.trim();
}

// click动作, 使用evaluate
function pagedown(page){
    console.log('start PageDown');
    page.evaluate(function(){
        document.getElementsByClassName('pager_next ')[0].click();
    });
    console.log('finish PageDown');
}

// click动作, 使用evaluateJavaScript
function pagedown_script(page){
    console.log('start PageDown');
    var script = "function(){document.getElementsByClassName('n')[0].click();}";
    page.evaluateJavaScript(script);
    console.log('finish PageDown');
}


page.open(url, function(status) {
    console.log('1HTTP Status: '+status);
    // print_title(page);
    // console.log(page.content);

    // console.log('2HTTP Status: '+status);
    //     document.getElementById('ess_ctr7042_SearchCenter_lbtnNext').click();
    // document.getElementsByClassName('commandbutton')[2].click();
    page.evaluate(function () {
        // document.getElementById('ess_ctr7042_SearchCenter_lbtnNext').click();
        document.getElementById('ess_ctr7042_SearchCenter_lblPageNumInfo').setAttribute('value','10');
        document.getElementById('ess_ctr7042_SearchCenter_lbtnToPage').click();
    });

    console.log(page.content);

    // var val = document.getElementById('ess_ctr7042_SearchCenter_lblPageNumInfo');
    // var val2 = val.getElementsByTagName('font')[0].textContent;
    // console.log(val2);
});

// function eval(page) {
//     page.evaluate(function (status) {
//         console.log('2HTTP Status: '+status);
//         // document.getElementById('ess_ctr7042_SearchCenter_lbtnNext').click();
//         document.getElementsByClassName('commandbutton')[2].click();
//
//         var val = document.getElementById('ess_ctr7042_SearchCenter_lblPageNumInfo');
//         var val2 = val.getElementsByTagName('font')[0].textContent;
//         console.log(val2);
//
//         page.evaluate(function (status) {
//             console.log('3HTTP Status: '+status);
//             document.getElementById('ess_ctr7042_SearchCenter_txtToPage').value="4";
//
//             page.evaluate(function (status) {
//                 console.log('4HTTP Status: '+status);
//                 document.getElementById('ess_ctr7042_SearchCenter_lbtnToPage').click();
//
//                 val = document.getElementById('ess_ctr7042_SearchCenter_lblPageNumInfo');
//                 val2 = val.getElementsByTagName('font')[0].textContent;
//                 console.log(val2);
//
//                 phantom.exit();
//
//             });
//         });
//
//     });
// }